package com.ych.modules.cms.authorInfc.dict.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.ych.modules.base.dbEntity.TSysDictEntity;
import com.ych.modules.common.service.DictService;
import com.ych.redis.RedisUtil;
import com.ych.utils.mysqlPageUtil.PageUtils;
import com.ych.utils.serviceReturn.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.util.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * Author: Usopp.tsui
 * Date: 2021/1/5
 * Time: 10:32
 * Description:
 */
@Api(tags = {"CMS--字典相关"}, description = "CMS--字典相关")
@RestController
@RequestMapping("/cms/auth/dict")
public class DictController {
    @Autowired
    private RedisUtil redisUtil;//redis查询

    @Autowired
    private DictService dictService;

    @ApiOperation(value = "获取公共字典type码值", position = 1)
    @GetMapping("/queryByType/{type}")
    public R<List<TSysDictEntity>> queryByType(@ApiParam(name = "type", value = "码值类型", required = true) @PathVariable("type") String type) {
        if (StringUtils.isBlank(type)) {
            return new R(1, "码值类型不可为空", null);
        }
        List<TSysDictEntity> dictEntityList = dictService.listDictByType(type);
        if (CollectionUtils.isEmpty(dictEntityList)) {
            throw new RuntimeException("码值类型不存在");
        }
        return new R(0, "获取成功", dictEntityList);
    }

    @ApiOperation(value = "获取公共字典types字符串数组码值", position = 2)
    @GetMapping("/queryByTypes/{types}")
    public R<Map<String, List<TSysDictEntity>>> queryByTypes(@ApiParam(name = "types", value = "码值类型", required = true) @PathVariable("types") String[] types) {
        try {
            if (null == types || 0 == types.length) {
                throw new RuntimeException("至少传入一个码值类型");
            }
            Map<String, List<TSysDictEntity>> rtMap = new HashMap<>();
            for (String type : types) {
                List<TSysDictEntity> list = dictService.listDictByType(type);
                if (CollectionUtils.isEmpty(list)) {
                    return new R(2, "码值类型不存在[ " + type + " ]", null);
                }
                rtMap.put(type, list);
            }
            return new R(0, "成功", rtMap);
        } catch (Exception e) {
            return new R(1, e.getMessage(), null);
        }
    }

    @RequiresPermissions("cms:auth:dict")
    @ApiOperation(value = "保存数据字典", notes = "", position = 2)
    @PostMapping("/saveDict")
    public R<String> save(@RequestBody TSysDictEntity tSysDictEntity) {
        try {
            if (StringUtils.isBlank(tSysDictEntity.getValue())) {
                throw new RuntimeException("value不可为空");
            }
            if (StringUtils.isBlank(tSysDictEntity.getType())) {
                throw new RuntimeException("类型不可为空");
            }
            if (StringUtils.isBlank(tSysDictEntity.getLabel())) {
                throw new RuntimeException("标签不可为空");
            }
            if (StringUtils.isBlank(tSysDictEntity.getValue())) {
                throw new RuntimeException("value不可为空");
            }
            if (null == tSysDictEntity.getLevel()) {
                throw new RuntimeException("权重不可为空");
            }

            if (StringUtils.isBlank(tSysDictEntity.getRemarks())) {
                throw new RuntimeException("备注不可为空");
            }
            dictService.saveDict(tSysDictEntity);
            return new R(0, "保存成功!", null);
        } catch (RuntimeException e) {
            return new R(1, e.getMessage(), null);
        }
    }

    @RequiresPermissions("cms:auth:dict")
    @ApiOperation(value = "查询数据字典", notes = "查询数据字典", position = 3)
    @PostMapping("/selectDict")
    public R<Page<TSysDictEntity>> selectDict(@RequestBody TSysDictEntity tSysDictEntity) {
        try {
            Page<TSysDictEntity> page = dictService.selectDict(tSysDictEntity);
            return new R(0, "查询成功", new PageUtils(page));
        } catch (Exception e) {
            return new R(1, e.getMessage(), null);
        }
    }

    @ApiOperation(value = "更新数据字典", notes = "", position = 4)
    @PostMapping("/updateDict")
    @RequiresPermissions("cms:auth:dict")
    public R<String> updateDict(@RequestBody TSysDictEntity tSysDictEntity) {
        try {
            if (StringUtils.isBlank(tSysDictEntity.getDictId())) {
                throw new RuntimeException("id不可为空");
            }
            if (StringUtils.isBlank(tSysDictEntity.getValue())) {
                throw new RuntimeException("value不可为空");
            }
            if (StringUtils.isBlank(tSysDictEntity.getType())) {
                throw new RuntimeException("类型不可为空");
            }
            if (StringUtils.isBlank(tSysDictEntity.getLabel())) {
                throw new RuntimeException("标签不可为空");
            }
            if (null == tSysDictEntity.getLevel()) {
                throw new RuntimeException("权重不可为空");
            }
            if (StringUtils.isBlank(tSysDictEntity.getRemarks())) {
                throw new RuntimeException("备注不可为空");
            }
            dictService.updateDict(tSysDictEntity);
            return new R(0, "修改成功", null);
        } catch (Exception e) {
            return new R(1, e.getMessage(), e);
        }
    }

    @ApiOperation(value = "删除数据字典", notes = "", position = 5)
    @PostMapping("/removeDict")
    @RequiresPermissions("cms:auth:dict")
    public R<String> removeDict(@RequestBody String dictId) {
        try {
            if (StringUtils.isBlank(dictId)) {
                throw new RuntimeException("id不可为空");
            }
            dictService.removeDict(dictId);
            return new R(0, "删除成功", null);
        } catch (Exception e) {
            return new R(1, e.getMessage(), null);
        }
    }
}
